# Estimate the conditional effects of average populist score between the top two presidential parties.
# See Figure N.2, Appendix N.




#############
# load data #
#############

library(brms)
library(ggplot2)
library(ggpubr)
library(stargazer)

load("PSRM Replication Files/PresidentialElectionsPosts.RData")

colnames(president_df)

all(c("populist_election_average", "polarization") %in% colnames(president_df)) # TRUE

length(unique(president_df$country)) # 26
length(unique(president_df$electionname)) # 29
length(unique(president_df$party_country)) # 52

# focus on 15 days before and after the election
day_range <- 15

sub <- president_df[which(president_df$daysinceelection >= -1*day_range
                          & president_df$daysinceelection <= day_range),]

# winners and losers
winners <- sub[which(sub$win == 1),]
losers <- sub[which(sub$win != 1),]

length(unique(winners$electionname[which(!is.na(winners$polarization))])) # 27
length(unique(winners$electionname[which(!is.na(winners$economic_polarization))])) # 27
length(unique(winners$electionname[which(!is.na(winners$social_polarization))])) # 27
length(unique(winners$electionname[which(!is.na(winners$populist_present))])) # 27
length(unique(winners$electionname[which(!is.na(winners$populist_dummy))])) # 27

length(unique(losers$electionname[which(!is.na(losers$polarization))])) # 27
length(unique(losers$electionname[which(!is.na(losers$economic_polarization))])) # 27
length(unique(losers$electionname[which(!is.na(losers$social_polarization))])) # 27
length(unique(losers$electionname[which(!is.na(losers$populist_present))])) # 27
length(unique(losers$electionname[which(!is.na(losers$populist_dummy))])) # 27




##################
# winners + love #
##################

winner_love2 <- brm(loveprop ~ post*populist_election_average + post*polarization
                    + incumbentparty + concurrent + runoff + semipresidential + enc_v1
                    + Xl + Xr 
                    + (1 + Xl + Xr | party_election),
                    data=winners,
                    warmup=1000, iter=2000, chains=3, seed=1234)
#plot(winner_love2)
#summary(winner_love2)




###################
# winners + angry #
###################

winner_angry2 <- brm(angryprop ~ post*populist_election_average + post*polarization
                     + incumbentparty + concurrent + runoff + semipresidential + enc_v1
                     + Xl + Xr 
                     + (1 + Xl + Xr | party_election),
                     data=winners,
                     warmup=1000, iter=2000, chains=3, seed=1234)
#plot(winner_angry2)
#summary(winner_angry2)




#################
# losers + love #
#################

loser_love2 <- brm(loveprop ~ post*populist_election_average + post*polarization
                   + incumbentparty + concurrent + runoff + semipresidential + enc_v1
                   + Xl + Xr 
                   + (1 + Xl + Xr | party_election),
                   data=losers,
                   warmup=1000, iter=2000, chains=3, seed=1234)
#plot(loser_love2)
#summary(loser_love2)




##################
# losers + angry #
##################

loser_angry2 <- brm(angryprop ~ post*populist_election_average + post*polarization
                    + incumbentparty + concurrent + runoff + semipresidential + enc_v1
                    + Xl + Xr 
                    + (1 + Xl + Xr | party_election),
                    data=losers,
                    warmup=1000, iter=2000, chains=3, seed=1234)
#plot(loser_angry2)
#summary(loser_angry2)

#save(winner_love2,
#     winner_angry2,
#     loser_love2,
#     loser_angry2,
#     file="Stan_PopulismAverage.RData")




##################
# result summary #
##################

load("PSRM Replication Files/Stan_PopulismAverage.RData")


# plot interaction terms
getStdPostCI <- function(model, var1, var2, intr, label){
  coeftab <- summary(model)[[14]][,c(1,3,4)]
  coeftab <- coeftab[intr,]
  
  temp <- data.frame(label=label,
                     est=coeftab[1],
                     lwr=coeftab[2],
                     upr=coeftab[3])
  colnames(temp) <- c("label", "est", "lwr", "upr")
  return(temp)
}

citab <- rbind(getStdPostCI(winner_love2, 2, 3, 12, "Winner + Love"),
               getStdPostCI(winner_love2, 2, 4, 13, "Winner + Love"),
               getStdPostCI(winner_angry2, 2, 3, 12, "Winner + Angry"),
               getStdPostCI(winner_angry2, 2, 4, 13, "Winner + Angry"),
               getStdPostCI(loser_love2, 2, 3, 12, "Loser + Love"),
               getStdPostCI(loser_love2, 2, 4, 13, "Loser + Love"),
               getStdPostCI(loser_angry2, 2, 3, 12, "Loser + Angry"),
               getStdPostCI(loser_angry2, 2, 4, 13, "Loser + Angry"))

citab$intr <- rep(c("(a) Post Election × Average Populism", "(b) Post Election × Polarization"),
                  times=4)

citab$label <- factor(citab$label, levels=rev(unique(citab$label)))
citab$intr <- factor(citab$intr, levels=unique(citab$intr))

# figure N.2
ggplot(data = citab, 
       aes(x = est, y = label)) + 
  facet_wrap(. ~ intr, ncol = 2, scales = "free_x") +
  geom_errorbar(data = citab, aes(xmin = lwr, xmax = upr),
                lwd=0.8, width = 0.1) +
  geom_vline(xintercept=0, lwd=0.5, lty=2) +
  geom_point(cex=4, pch=19) +
  xlab("Posterior Mean and 95% Credible Interval") +
  ylab("") +
  theme_bw() +
  theme(axis.title.x = element_text(size = 15),
        axis.title.y = element_text(size = 15),
        axis.text=element_text(size=12),
        panel.spacing = unit(1, "lines"))

#ggsave("interaction_averagepopulism.pdf", width=12, height=6)
